package com.cx.service.impl;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cx.mapper.CommentMapper;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.cx.entity.Comment;
import com.cx.service.CommentService;

import java.util.Map;


@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {


    @Override
    public IPage<Comment> getCommentListAndArticlePageConditions(Map<String, Object> params) {
        IPage<Comment> page = new Page<>(Integer.parseInt((String) params.get("page")),Integer.parseInt((String) params.get("limit")));
        QueryWrapper<Comment> queryWrapper = new QueryWrapper<>();
        if (params.get("searchParams")!=null) {
//            将获取的json格式的字符串转为Java中的对象
            Map<String, String> searchParamsMap = (Map) (JSON.parse((String) params.get("searchParams")));
            String startDate = null;
            String endDate = null;
            System.out.println("========================="+searchParamsMap);
            if (!"".equals(searchParamsMap.get("created"))) {
                startDate = (searchParamsMap.get("created").split("~"))[0];
                endDate = (searchParamsMap.get("created").split("~"))[1];
            }
            queryWrapper.like(!"".equals(searchParamsMap.get("content")),"content",searchParamsMap.get("content"))
                    .ge(startDate!=null,"created",startDate)
                    .le(endDate!=null,"created",endDate)
                    .eq(!"".equals(searchParamsMap.get("author")),"author",searchParamsMap.get("author"));
        }
        return this.baseMapper.getCommentListAndArticlePageConditions(page,queryWrapper);
    }
}